<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD><TITLE>Tcl_CreateMathFunc manual page - Tcl Library Procedures</TITLE>
<link rel="stylesheet" href="../docs.css" type="text/css" media="all">
</HEAD>
<BODY><H2><a href="../contents.htm">Tcl8.6.11/Tk8.6.11 Documentation</a> <small>&gt;</small> <a href="contents.htm">Tcl C API</a> <small>&gt;</small> CrtMathFnc</H2>
<H3><A HREF="../UserCmd/contents.htm">Tcl/Tk Applications</A> | <A HREF="../TclCmd/contents.htm">Tcl Commands</A> | <A HREF="../TkCmd/contents.htm">Tk Commands</A> | <A HREF="../ItclCmd/contents.htm">[incr Tcl] Package Commands</A> | <A HREF="../SqliteCmd/contents.htm">SQLite3 Package Commands</A> | <A HREF="../TdbcCmd/contents.htm">TDBC Package Commands</A> | <A HREF="../TdbcmysqlCmd/contents.htm">tdbc::mysql Package Commands</A> | <A HREF="../TdbcodbcCmd/contents.htm">tdbc::odbc Package Commands</A> | <A HREF="../TdbcpostgresCmd/contents.htm">tdbc::postgres Package Commands</A> | <A HREF="../TdbcsqliteCmd/contents.htm">tdbc::sqlite3 Package Commands</A> | <A HREF="../ThreadCmd/contents.htm">Thread Package Commands</A> | <A HREF="../TclLib/contents.htm">Tcl C API</A> | <A HREF="../TkLib/contents.htm">Tk C API</A> | <A HREF="../ItclLib/contents.htm">[incr Tcl] Package C API</A> | <A HREF="../TdbcLib/contents.htm">TDBC Package C API</A></H3>
<DL>
<DD><A HREF="CrtMathFnc.htm#M2" NAME="L173">NAME</A>
<DL><DD>Tcl_CreateMathFunc, Tcl_GetMathFuncInfo, Tcl_ListMathFuncs &mdash; Define, query and enumerate math functions for expressions</DD></DL>
<DD><A HREF="CrtMathFnc.htm#M3" NAME="L174">NOTICE OF EVENTUAL DEPRECATION</A>
<DD><A HREF="CrtMathFnc.htm#M4" NAME="L175">SYNOPSIS</A>
<DL>
<DD><B>#include &lt;tcl.h&gt;</B>
<DD>void
<DD><B>Tcl_CreateMathFunc</B>(<I>interp, name, numArgs, argTypes, proc, clientData</I>)
<DD>int
<DD><B>Tcl_GetMathFuncInfo</B>(<I>interp, name, numArgsPtr, argTypesPtr, procPtr,
<DD>clientDataPtr</I>)
<DD>Tcl_Obj *
<DD><B>Tcl_ListMathFuncs</B>(<I>interp, pattern</I>)
</DL>
<DD><A HREF="CrtMathFnc.htm#M5" NAME="L176">ARGUMENTS</A>
<DL class="arguments">
</DL>
<DD><A HREF="CrtMathFnc.htm#M6" NAME="L177">DESCRIPTION</A>
<DD><A HREF="CrtMathFnc.htm#M7" NAME="L178">SEE ALSO</A>
<DD><A HREF="CrtMathFnc.htm#M8" NAME="L179">KEYWORDS</A>
</DL>
<H3><A NAME="M2">NAME</A></H3>
Tcl_CreateMathFunc, Tcl_GetMathFuncInfo, Tcl_ListMathFuncs &mdash; Define, query and enumerate math functions for expressions
<H3><A NAME="M3">NOTICE OF EVENTUAL DEPRECATION</A></H3>
The <B>Tcl_CreateMathFunc</B> and <B>Tcl_GetMathFuncInfo</B> functions
are rendered somewhat obsolete by the ability to create functions for
expressions by placing commands in the <B>tcl::mathfunc</B> namespace,
as described in the <B><A HREF="../TclCmd/mathfunc.htm">mathfunc</A></B> manual page; the API described on
this page is not expected to be maintained indefinitely.
<H3><A NAME="M4">SYNOPSIS</A></H3>
<B>#include &lt;tcl.h&gt;</B><BR>
void<BR>
<B>Tcl_CreateMathFunc</B>(<I>interp, name, numArgs, argTypes, proc, clientData</I>)<BR>
int<BR>
<B>Tcl_GetMathFuncInfo</B>(<I>interp, name, numArgsPtr, argTypesPtr, procPtr,<BR>
<TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TT>clientDataPtr</I>)<BR>
<A HREF="../TclLib/Object.htm">Tcl_Obj</A> *<BR>
<B>Tcl_ListMathFuncs</B>(<I>interp, pattern</I>)<BR>
<H3><A NAME="M5">ARGUMENTS</A></H3>
<DL class="arguments">
<DT><A HREF="../TclLib/Interp.htm">Tcl_Interp</A> <B>*interp</B> (in)<DD>
Interpreter in which new function will be defined.
<P><DT>const char <B>*name</B> (in)<DD>
Name for new function.
<P><DT>int <B>numArgs</B> (in)<DD>
Number of arguments to new function;  also gives size of <I>argTypes</I> array.
<P><DT>Tcl_ValueType <B>*argTypes</B> (in)<DD>
Points to an array giving the permissible types for each argument to
function.
<P><DT>Tcl_MathProc <B>*proc</B> (in)<DD>
Procedure that implements the function.
<P><DT>ClientData <B>clientData</B> (in)<DD>
Arbitrary one-word value to pass to <I>proc</I> when it is invoked.
<P><DT>int <B>*numArgsPtr</B> (out)<DD>
Points to a variable that will be set to contain the number of
arguments to the function.
<P><DT>Tcl_ValueType <B>**argTypesPtr</B> (out)<DD>
Points to a variable that will be set to contain a pointer to an array
giving the permissible types for each argument to the function which
will need to be freed up using <I><A HREF="../TclLib/Alloc.htm">Tcl_Free</A></I>.
<P><DT>Tcl_MathProc <B>**procPtr</B> (out)<DD>
Points to a variable that will be set to contain a pointer to the
implementation code for the function (or NULL if the function is
implemented directly in bytecode).
<P><DT>ClientData <B>*clientDataPtr</B> (out)<DD>
Points to a variable that will be set to contain the clientData
argument passed to <I>Tcl_CreateMathFunc</I> when the function was
created if the function is not implemented directly in bytecode.
<P><DT>const char <B>*pattern</B> (in)<DD>
Pattern to match against function names so as to filter them (by
passing to <I><A HREF="../TclLib/StrMatch.htm">Tcl_StringMatch</A></I>), or NULL to not apply any filter.
<P></DL>
<H3><A NAME="M6">DESCRIPTION</A></H3>
Tcl allows a number of mathematical functions to be used in
expressions, such as <B>sin</B>, <B>cos</B>, and <B>hypot</B>.
These functions are represented by commands in the namespace,
<B>tcl::mathfunc</B>.  The <B>Tcl_CreateMathFunc</B> function is
an obsolete way for applications to add additional functions
to those already provided by Tcl or to replace existing functions.
It should not be used by new applications, which should create
math functions using <B><A HREF="../TclLib/CrtObjCmd.htm">Tcl_CreateObjCommand</A></B> to create a command
in the <B>tcl::mathfunc</B> namespace.
<P>
In the <B>Tcl_CreateMathFunc</B> interface,
<I>Name</I> is the name of the function as it will appear in expressions.
If <I>name</I> does not already exist in the <B>::tcl::mathfunc</B>
namespace, then a new command is created in that namespace.
If <I>name</I> does exist, then the existing function is replaced.
<I>NumArgs</I> and <I>argTypes</I> describe the arguments to the function.
Each entry in the <I>argTypes</I> array must be
one of <B>TCL_INT</B>, <B>TCL_DOUBLE</B>, <B>TCL_WIDE_INT</B>,
or <B>TCL_EITHER</B> to indicate whether the corresponding argument must be an
integer, a double-precision floating value, a wide (64-bit) integer,
or any, respectively.
<P>
Whenever the function is invoked in an expression Tcl will invoke
<I>proc</I>.  <I>Proc</I> should have arguments and result that match
the type <B>Tcl_MathProc</B>:
<P>
<PRE>typedef int <B>Tcl_MathProc</B>(
        ClientData <I>clientData</I>,
        <A HREF="../TclLib/Interp.htm">Tcl_Interp</A> *<I>interp</I>,
        Tcl_Value *<I>args</I>,
        Tcl_Value *<I>resultPtr</I>);</PRE>
<P>
When <I>proc</I> is invoked the <I>clientData</I> and <I>interp</I>
arguments will be the same as those passed to <B>Tcl_CreateMathFunc</B>.
<I>Args</I> will point to an array of <I>numArgs</I> Tcl_Value structures,
which describe the actual arguments to the function:
<P>
<PRE>typedef struct Tcl_Value {
    Tcl_ValueType <I>type</I>;
    long <I>intValue</I>;
    double <I>doubleValue</I>;
    <A HREF="../TclLib/IntObj.htm">Tcl_WideInt</A> <I>wideValue</I>;
} <B>Tcl_Value</B>;</PRE>
<P>
The <I>type</I> field indicates the type of the argument and is
one of <B>TCL_INT</B>, <B>TCL_DOUBLE</B> or <B>TCL_WIDE_INT</B>.
It will match the <I>argTypes</I> value specified for the function unless
the <I>argTypes</I> value was <B>TCL_EITHER</B>. Tcl converts
the argument supplied in the expression to the type requested in
<I>argTypes</I>, if that is necessary.
Depending on the value of the <I>type</I> field, the <I>intValue</I>,
<I>doubleValue</I> or <I>wideValue</I>
field will contain the actual value of the argument.
<P>
<I>Proc</I> should compute its result and store it either as an integer
in <I>resultPtr-&gt;intValue</I> or as a floating value in
<I>resultPtr-&gt;doubleValue</I>.
It should set also <I>resultPtr-&gt;type</I> to one of
<B>TCL_INT</B>, <B>TCL_DOUBLE</B> or <B>TCL_WIDE_INT</B>
to indicate which value was set.
Under normal circumstances <I>proc</I> should return <B><A HREF="../TclCmd/catch.htm">TCL_OK</A></B>.
If an error occurs while executing the function, <I>proc</I> should
return <B><A HREF="../TclCmd/catch.htm">TCL_ERROR</A></B> and leave an error message in the interpreter's result.
<P>
<B>Tcl_GetMathFuncInfo</B> retrieves the values associated with
function <I>name</I> that were passed to a preceding
<B>Tcl_CreateMathFunc</B> call.  Normally, the return code is
<B><A HREF="../TclCmd/catch.htm">TCL_OK</A></B> but if the named function does not exist, <B><A HREF="../TclCmd/catch.htm">TCL_ERROR</A></B>
is returned and an error message is placed in the interpreter's
result.
<P>
If an error did not occur, the array reference placed in the variable
pointed to by <I>argTypesPtr</I> is newly allocated, and should be
released by passing it to <B><A HREF="../TclLib/Alloc.htm">Tcl_Free</A></B>.  Some functions (the
standard set implemented in the core, and those defined by placing
commands in the <B>tcl::mathfunc</B> namespace) do not have
argument type information; attempting to retrieve values for
them causes a NULL to be stored in the variable pointed to by
<I>procPtr</I> and the variable pointed to by <I>clientDataPtr</I>
will not be modified.  The variable pointed to by <I>numArgsPointer</I>
will contain -1, and no argument types will be stored in the variable
pointed to by <I>argTypesPointer</I>.
<P>
<B>Tcl_ListMathFuncs</B> returns a Tcl value containing a list of all
the math functions defined in the interpreter whose name matches
<I>pattern</I>.  The returned value has a reference count of zero.
<H3><A NAME="M7">SEE ALSO</A></H3>
<B><A HREF="../TclCmd/expr.htm">expr</A></B>, <B><A HREF="../TclCmd/info.htm">info</A></B>, <B><A HREF="../TclLib/CrtObjCmd.htm">Tcl_CreateObjCommand</A></B>, <B><A HREF="../TclLib/Alloc.htm">Tcl_Free</A></B>, <B><A HREF="../TclLib/ListObj.htm">Tcl_NewListObj</A></B>
<H3><A NAME="M8">KEYWORDS</A></H3>
<A href="../Keywords/E.htm#expression">expression</A>, <A href="../Keywords/M.htm#mathematical function">mathematical function</A>
<div class="copy">Copyright &copy; 1989-1993 The Regents of the University of California.
<BR>Copyright &copy; 1994-1996 Sun Microsystems, Inc.
</div>
</BODY></HTML>
